//
//  PKReadingScouView.m
//  PianKe
//
//  Created by mingtao on 15/10/9.
//  Copyright (c) 2015年 mingtao. All rights reserved.
//

#import "PKReadingScouView.h"

#import <UIImageView+WebCache.h>

@interface PKReadingScouView()<UIScrollViewDelegate>

@end

@implementation PKReadingScouView

-(instancetype) initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    if (self) {
        _ScroView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, WIHES, HHIHEI/6+40)];
        _ScroView.backgroundColor = [UIColor redColor];
        //设置UIScrollview大小
        _ScroView.contentSize = CGSizeMake(WIHES*3, 0);
        
        //每次翻动的屏幕大小的宽度(表示翻动与屏幕大小整屏翻动)
        _ScroView.pagingEnabled = YES;
  
        _ScroView.delegate = self;
        
        //隐藏滑动条
       // _ScroView.showsHorizontalScrollIndicator=NO;
        
        for (int i = 0; i<3; i++) {
            _imageV = [[UIImageView  alloc]initWithFrame:CGRectMake(WIHES*i, 0, WIHES, HHIHEI/6+40)];
            
            _imageV.userInteractionEnabled=YES;
            
            [_ScroView addSubview:_imageV];
        }
        
        [self addSubview:_ScroView];
    
        _page = [[UIPageControl alloc]initWithFrame:CGRectMake(220, HHIHEI/6, 100, 30)];
        
        _page.numberOfPages = 3;
        _page.currentPage=0;
        _page.currentPageIndicatorTintColor = [UIColor orangeColor];
        
        [_page addTarget:self action:@selector(pageclike:) forControlEvents:UIControlEventValueChanged];
        
        
        [self addSubview:_page];
        

        

        
    }
    
    return self;

}

-(void)setRedingModel:(PKReadingModel *)redingModel
{
    _redingModel = redingModel;
    
    for (int i = 0; i<redingModel.carousel.count; i++) {
        UIImageView *imageS=_ScroView.subviews[i];
        
        [imageS sd_setImageWithURL:[NSURL URLWithString:((PKReadingCarousel *)redingModel.carousel[i]).img]];
      
        
        imageS.tag = 1000+i;
        
    #pragma mark 设置手势点击事件
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapView:)];
        
        [imageS addGestureRecognizer:tap];
        
    }
    
    [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];

    



}

-(void)nextImage
{
    if (_page.currentPage<2) {
        _page.currentPage++;
        [self pageclike:_page];
    }else if (_page.currentPage==2)
    {
        _page.currentPage=0;
        [self pageclike:_page];
    
    }

}



- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    _page.currentPage = scrollView.contentOffset.x/scrollView.bounds.size.width;
    
}


-(void)pageclike:(UIPageControl *)page
{
    _ScroView.contentOffset = CGPointMake(page.currentPage*self.bounds.size.width, 0);

}




-(void)tapView:(UITapGestureRecognizer *)tap
{
    
//    if (self.imagetagBlock!=nil) {
        self.imagetagBlock(tap.view.tag-1000);
       // NSLog(@"%lu",tap.view.tag-1000);
//    }
    
   
    
//    if (tap.view.tag ==1001)
//    {
//        NSLog(@"%lu",tap.view.tag);
//        
//    }else if (tap.view.tag==1002)
//    {
//        NSLog(@"%lu",tap.view.tag);
//        
//    }else if (tap.view.tag==1003)
//    {
//        NSLog(@"%lu",tap.view.tag);
//        
//    }
    
}






/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
